package com.tangkun.environment.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.adapter.WoTiJiaoDeAdapter;
import com.tangkun.environment.entity.xinwen.QuestionIndexReturn;
import com.tangkun.environment.network.MySubscribe;
import com.tangkun.environment.rx.RxActivityHelper;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.view.CustomListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我提交的 列表
 */

public class WoTiJiaoDeActivity extends BaseActivity implements CustomListView.OnLoadMoreListener, CustomListView.OnRefreshListener {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.custom_list_view)
    CustomListView customListView;
    private Context mContext;
    /**
     * 用户ID
     */
    private String id = "";

    public int page = 1;// 当前页
    private int totalCount = 0;// 总数量
    private boolean OnHeadRefreshTAG = true;// 为true时清楚已有数据

    /**
     * 数据源
     */
    private List<QuestionIndexReturn.DataBean.ListBean> dataList = new ArrayList<QuestionIndexReturn.DataBean.ListBean>();
    /**
     * 适配器
     */
    private WoTiJiaoDeAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        id = getIntent().getStringExtra("id");
        setContentView(R.layout.activity_common_customlistview);
        ButterKnife.bind(this);
        initView();
        title.setText("我提交的");
        queryIndexQuestion();
    }

    private void initView() {
        customListView.setOnRefreshListener(this);
        customListView.setOnLoadListener(this);
        customListView.setCanLoadMore(true);
        customListView.setAutoLoadMore(true);
        adapter = new WoTiJiaoDeAdapter(WoTiJiaoDeActivity.this, dataList);
        customListView.setAdapter(adapter);
    }

    @OnClick(R.id.back)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                JumpHelper.finish(mContext);
                break;
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        OnHeadRefreshTAG = true;
        customListView.setCanLoadMore(true);
        queryIndexQuestion();
    }

    @Override
    public void onLoadMore() {
        OnHeadRefreshTAG = false;
        if (page * pageSize < totalCount) {
            page++;
            queryIndexQuestion();
        } else {
            customListView.onLoadMoreCompleteNoData();
        }
    }


    /**
     * 我提交的咨询列表
     */
    private void queryIndexQuestion() {
        Map<String, String> map = new HashMap<>();
        map.put("pageNumber", page + "");
        map.put("pageSize", pageSize + "");
        fromNetwork.queryIndexQuestion(map).compose(new RxActivityHelper<QuestionIndexReturn>().ioMain(WoTiJiaoDeActivity.this, true))
                .subscribe(new MySubscribe<QuestionIndexReturn>(mContext, true) {
                    @Override
                    protected void success(QuestionIndexReturn resultBean) {
                        if (resultBean.data != null) {
                            customListView.onLoadMoreComplete();
                            customListView.onRefreshComplete();
                            customListView.setCanLoadMore(true);
                            totalCount = resultBean.data.totalRow;
                            if (page * pageSize >= totalCount) {
                                customListView.setCanLoadMore(false);
                            }
                            //下拉刷新
                            if (OnHeadRefreshTAG) {
                                OnHeadRefreshTAG = false;
                                dataList.clear();
                            }
                            //填充数据
                            dataList.addAll(resultBean.data.list);
                            //刷新列表
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage);
                    }
                });
    }
}
